package linkedlist.test2;

import listbyorder.utils.ListNode;

/**
 *  两数相加
 * @author code_yc
 * @version 1.0
 * @date 2020/7/14 13:45
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode t = dummy;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 == null ? 0 : l1.val;
            int n2 = l2 == null ? 0 : l2.val;
            int sum = n1 + n2 + carry;
            int c = sum % 10;
            carry = sum / 10;
            t.next = new ListNode(c);
            t = t.next;
            l1 = l1 == null ? l1 : l1.next;
            l2 = l2 == null ? l2 : l2.next;
        }
        if (carry != 0) {
            t.next = new ListNode(carry);
        }
        return dummy.next;
    }
}
